package com.kingint.web.admin.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import com.kingint.web.sys.service.UmsMemberService;
import com.kingint.web.util.UserIdUtil;
import com.kingint.web.util.validateUtil.ValidList;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.admin.base.service.BasHotelService;
import com.kingint.mbg.bas.entity.BasHotel;

import java.util.List;


/**
 * @描述：酒店表控制类
 * @作者: wxh
 * @日期: 2020-12-21
 */
@RestController
@RequestMapping("/basHotel")
@Api(value = "BasHotelController", description = "酒店表")
@CrossOrigin
public class BasHotelController{

    @Autowired
    public BasHotelService basHotelService;
    @Autowired
    private UserIdUtil userIdUtil;
    @Autowired
    private UmsMemberService umsMemberService;


    @ApiOperation(value = "添加酒店表")
    @PostMapping("/create")
    @PreAuthorize("hasAuthority('basHotel:create')")
    public Object create(@Validated(Insert.class) @RequestBody BasHotel basHotel){
        Boolean bool=basHotelService.saveOrUpdate(basHotel);
        if(bool){
            return K.ok().data(basHotel.getId());
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "批量添加酒店表")
    @PostMapping("/insertList")
    @PreAuthorize("hasAuthority('basHotel:insertList')")
    public Object create(@RequestBody @Validated(Insert.class)  ValidList<BasHotel> basHotel){
        Boolean bool=basHotelService.saveOrUpdateBatch(basHotel);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "更新酒店表")
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('basHotel:update')")
    public Object update(@Validated(Update.class) @RequestBody BasHotel basHotel){
        Boolean bool= basHotelService.updateById(basHotel);
        if(bool){
             return K.ok().data(basHotel.getId());
        }else{
             return K.error();
        }
    }


    @ApiOperation(value = "删除酒店表")
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('basHotel:delete')")
    public Object delete(@PathVariable("id") Long id){
        Boolean bool= basHotelService.removeById(id);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "根据酒店表名称分页获取酒店表列表")
    @GetMapping("/listAll")
    @PreAuthorize("hasAuthority('basHotel:list')")
    public Object getListAll(BasHotel basHotel,
    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        Page<BasHotel> page = new Page<>(pageNum, pageSize);
        IPage<BasHotel> info=basHotelService.page(page,null);
        return K.ok().data(info);
    }

    @ApiOperation(value = "根据酒店表名称分页获取酒店表列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('basHotel:list')")
    public Object getList(){
        QueryWrapper<BasHotel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",umsMemberService.getUserHotel());
        List<BasHotel> list = basHotelService.list(queryWrapper);
        return K.ok().data(list);
    }

    @ApiOperation(value = "根据编号查询酒店表信息")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('basHotel:get')")
    public Object getItem(@PathVariable("id") Long id){
        BasHotel basHotel=basHotelService.getById(id);
        return K.ok().data(basHotel);
    }
}